<?php

/*
 * Action -2+$
 *
 * Trash a card from your hand. Gain 2 cards each costing less than it.
 * When you buy this, you may overpay for it. If you do, gain 2 Action cards each costing the amount you overpaid.
 * 
 */

class Stonemason extends Card implements iAction, iKingdom, iGuilds {

    public function __construct() {
        parent::__construct("Stonemason", 2);
        $this->undoPlay = false;
        $this->undoBuy = false;
    }

    public function getDescription() {
        return array(
            "Trash a card from your hand. Gain 2 cards each costing less than it.",
            "When you buy this, you may overpay for it. If you do, gain 2 Action cards each costing the amount you overpaid.");
    }

    public function buy(Game $game) {
        $overpay = $game->getOverpayAmount($this->getValue(), null, true);
        if ($overpay != "None") {
            $maxPrice = new Price($overpay);
            $this->handleGainOfActionCards($game, 2, $maxPrice, $maxPrice);
        }
    }

    public function play(Game $game, $isRealCard) {
        $playhand = $game->getPlayHand();
        if (sizeof($playhand) > 0) {

            $playhand = getUniqueCards($playhand);
            if (sizeof($playhand) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $playhand, 1, 1, null, "Choose a card to trash");
                $old_card_name = $output[0];
            } else {
                $old_card_name = $playhand[0];
            }

            $old_card = $game->moveCardFromPileToPile($old_card_name, PlayHand, Trash);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($old_card), "trashes " . ScreenDisplay::$LMSG);

            $minPrice = new Price(0);
            $maxPrice = $old_card->getPrice($game)->add(-1);

            $this->handleGainOfActionCards($game, 2, $minPrice, $maxPrice);
        }
    }

    private function handleGainOfActionCards(Game $game, $numOfCards, $minPrice, $maxPrice) {
        for ($i = 0; $i < $numOfCards; $i+=1) {
            $optional_cards = $game->getBoardActionCardsByPrice($minPrice, $maxPrice);
            ksort($optional_cards);
            if (sizeof($optional_cards) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards, 1, 1, null, "Choose a card to gain");

                $new_card_name = $output[0];
                $new_card = $game->moveCardFromPileToPile($new_card_name, BoardCards, DiscardPile);
                if ($i == $numOfCards - 1) {
                    ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG);
                } else {
                    ScreenDisplay::addScreenMessage($game, false, ", gain " . nicePrint($new_card), ", gains " . ScreenDisplay::$LMSG);
                }
            } elseif (sizeof($optional_cards) == 1) {
                $new_card_name = $optional_cards[0];
                $new_card = $game->moveCardFromPileToPile($new_card_name, BoardCards, DiscardPile);
                if ($i == $numOfCards - 1) {
                    ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG);
                } else {
                    ScreenDisplay::addScreenMessage($game, false, ", gain " . nicePrint($new_card), ", gains " . ScreenDisplay::$LMSG);
                }
            }
        }
    }

}

?>